﻿using ConsoleApplication2.Entity;
using ConsoleApplication2.pushApi;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2.Dao
{
    /// <summary>
    ///  5分钟执行一次
    /// </summary>
    public class ExamRecordCmd
    {


        public static void checkData()
        {
            int[] intnumber = new int[] { 0, 1 };
            foreach (var item in intnumber)
            {
                checkData(item);
            }
        }

        /// <summary>
        /// 系统为您交卷
        /// </summary>
        /// <param name="type"></param>
        public static void checkData(int type)
        {
            string sql = String.Format("select top 1 TotalTime from T_MakePaperRoles where IsUsed=1 and [type]={0} and [Year]={0}", type);
            int timeLong = MyDataHelp.ExecuteScalar(sql);

            sql = String.Format(@"select * from T_ExamRecord as a 
                   where a.[Status]=1 and a. [Year]={0} and a.[Type]={1}   and 
                      CreateTime >dateAdd(minute,{2},getdate())", DateTime.Now.Year, type, "-" + timeLong);

            DataTable dt = MyDataHelp.GetDataTableBySQL(sql);

            String userID ="";
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (userID.Length == 0)
                    {
                        userID = dr[1].ToString();
                    }
                    else
                    {
                        userID += "," + dr[1].ToString();
                    }

                    String json = String.Format(@"{'ExamId':'{0}'}", dr[6]);
                    HttpClientHelp.sendMessage(HttpAddress.SubmiExamtPaper, json);
                }
            }

            if (!string.IsNullOrEmpty(userID))
            {
                MessagePara par = new MessagePara();
                par.UserIds = userID;
                par.Title = "同学你好，你刚刚参加了考试，尚未交卷，考试时间已到，系统将自动为你交卷";
                par.MessageType = 2;
                String json = JsonHelper.SerializeObject(par);
                string url = HttpAddress.personAddress;
                HttpClientHelp.sendMessage(url, json);
            }




        }
    }
}
